package com.help10000.rms.ui.activitys

import android.os.Bundle
import android.view.View
import com.amir.common.beans.ApplyInitEntity
import com.amir.common.mvp.view.IView
import com.amir.common.utils.DisplayUtil
import com.amir.common.utils.HideUtil
import com.amir.common.utils.ToastUtil
import com.amir.common.weight.DuplicateClickProxy
import com.help10000.rms.R
import com.help10000.rms.mvp.presenter.ApplyWorkPresenter
import com.help10000.rms.mvp.view.ApplyWorkView
import com.help10000.rms.ui.base.BaseActivity
import com.help10000.rms.ui.utils.PageUtils
import com.help10000.rms.ui.windows.TimePickerDialog
import com.lxj.xpopup.XPopup
import kotlinx.android.synthetic.main.activity_apply_work.*
import kotlinx.android.synthetic.main.layout_apply_idcart_info.*
import kotlinx.android.synthetic.main.layout_apply_live_info.*
import kotlinx.android.synthetic.main.layout_apply_other_info.*
import java.text.SimpleDateFormat
import java.util.*

/**
 * 申请入职
 */
class ApplyWorkActivity : BaseActivity<ApplyWorkPresenter, IView>(), ApplyWorkView,
    View.OnClickListener, TimePickerDialog.TimePickerDialogInterface {
    var mTimePickerDialog: TimePickerDialog? = null
    private var cmpyCode: String? = null
    private var applyData: MutableMap<String, String> = mutableMapOf()
    override fun getlayoutId(): Int {
        return R.layout.activity_apply_work
    }

    override fun onSaveInstanceState(outState: Bundle) {
        outState.putString("cmpycode", cmpyCode)
        super.onSaveInstanceState(outState)
    }

    override fun createPresenter(): ApplyWorkPresenter {
        return ApplyWorkPresenter(this)
    }

    override fun initView() {
        mTimePickerDialog = TimePickerDialog(getViewContext())
    }

    override fun initData(savedInstanceState: Bundle?) {
        if (savedInstanceState == null)
            cmpyCode = intent.getStringExtra("cmpycode")
        else
            cmpyCode = savedInstanceState.getString("cmpycode")

        mPresenter.applyInfoInit(cmpyCode)


    }

    override fun initListener() {
        birthdayTv.setOnClickListener(DuplicateClickProxy(this))
        genderTv.setOnClickListener(DuplicateClickProxy(this))
        marriegeTv.setOnClickListener(DuplicateClickProxy(this))
        educationTv.setOnClickListener(DuplicateClickProxy(this))
        provinceTv.setOnClickListener(DuplicateClickProxy(this))
        applyBtn.setOnClickListener(DuplicateClickProxy(this))
        applyDateTv.setOnClickListener(DuplicateClickProxy(this))
    }

    var applyEntity: ApplyInitEntity? = null
    override fun initInfoSuccess(applyEntity: ApplyInitEntity) {
        this.applyEntity = applyEntity
        companyCodeTv.setText(applyEntity.CmpyCode)
        companyNameTv.setText(applyEntity.CmpyName)
        mobileTv.setText(applyEntity.Mobile)

        val simpleDateFormat = SimpleDateFormat("yyyy-MM-dd", Locale.CHINA)
        applyDateTv.text = simpleDateFormat.format(Calendar.getInstance().time)
        applyData["InDate"] = simpleDateFormat.format(Calendar.getInstance().time)

        EmpNameTv.requestFocus()
        HideUtil.openKeyboard(mContext, EmpNameTv)
    }

    var chooseApplyDate = false
    override fun positiveListener() {
        var time = String.format(
            "%d-%02d-%02d",
            mTimePickerDialog?.year,
            mTimePickerDialog?.month,
            mTimePickerDialog?.day
        )


        if (chooseApplyDate) {
            chooseApplyDate = false
            applyData["InDate"] = time
            applyDateTv.text = time;
        } else {
            birthdayTv.setText(time)
            applyData["Birthday"] = time
        }
    }

    override fun negativeListener() {
        chooseApplyDate = false
    }

    override fun submitSuccess() {
        PageUtils.jumpToResult(this, BindEmployeeActivity::class.java, true, "提交成功")
        finish()
    }

    override fun onClick(v: View?) {
        when (v?.id) {
            R.id.birthdayTv -> {
                mTimePickerDialog?.showDatePickerDialog()
            }
            R.id.genderTv -> {
                XPopup.Builder(this)
                    .asBottomList("请选择", arrayOf("男", "女")) { position, text ->
                        genderTv.text = text
                        applyData["Gender"] = text
                    }.show()
            }
            R.id.marriegeTv -> {
                XPopup.Builder(this)
                    .asBottomList(
                        "婚姻状态",
                        applyEntity?.Marriege?.toTypedArray()
                    ) { position, text ->
                        marriegeTv.text = text
                        text.also { applyData["Marriege"] = it }
                    }.show()
            }
            R.id.educationTv -> {
                XPopup.Builder(this)
                    .asBottomList(
                        "教育程度",
                        applyEntity?.Edu?.toTypedArray()
                    ) { position, text ->
                        educationTv.text = text
                        text.also { applyData["Edu"] = it }
                    }.show()
            }
            R.id.applyDateTv -> {
                chooseApplyDate = true
                mTimePickerDialog?.showDatePickerDialog()
            }
            R.id.provinceTv -> {
                var provinceList = mutableListOf<String>()
                for (index in applyEntity!!.ProvInfos) {
                    provinceList.add(index.ProvName)
                }
                XPopup.Builder(this)
                    .maxHeight(DisplayUtil.getDensityHeight(this) / 2)
                    .asBottomList(
                        "籍贯",
                        provinceList.toTypedArray()
                    ) { position, text ->
                        provinceTv.text = text
                        text.also { applyData["ProvName"] = it }
                    }.show()
            }
            R.id.applyBtn -> {
                applyData["CmpyCode"] = cmpyCode!!
                applyData["EmpName"] = EmpNameTv.text.toString()
                applyData["Addr"] = addressTv.text.toString()
                applyData["Remark"] = remarkTv.text.toString()
                applyData["IDNo"] = idCodeTv.text.toString()
                if (applyData["EmpName"].isNullOrEmpty()) {
                    ToastUtil.show("请输入姓名")
                    EmpNameTv.requestFocus()
                    HideUtil.openKeyboard(this, EmpNameTv)
                    return
                }
                var phone = mobileTv.text.toString();
                var reg = Regex("1[3-9][0-9]{9}")
                if (!reg.matches(phone)) {
                    ToastUtil.show("手机号码输入有误")
                    mobileTv.requestFocus()
                    HideUtil.openKeyboard(this, mobileTv)
                    return
                }
                applyData["Tel"] = phone

                if (applyData["IDNo"].isNullOrEmpty()) {
                    ToastUtil.show("请输入身份证号")
                    idCodeTv.requestFocus()
                    HideUtil.openKeyboard(this, idCodeTv)
                    return
                } else {
                    val matched =
                        Regex("^[1-9]\\d{5}(18|19|([23]\\d))\\d{2}((0[1-9])|(10|11|12))(([0-2][1-9])|10|20|30|31)\\d{3}[0-9Xx]\$").containsMatchIn(
                            applyData["IDNo"]!!
                        )
                    if (!matched) {
                        ToastUtil.show("身份证格式输入有误")
                        idCodeTv.requestFocus()
                        HideUtil.openKeyboard(this, idCodeTv)
                        return
                    }
                }
                if (applyData["Gender"].isNullOrEmpty()) {
                    ToastUtil.show("请选择性别")
                    genderTv.performClick()
                    HideUtil.closeKeyboard(this)
                    return
                }
                if (applyData["Birthday"].isNullOrEmpty()) {
                    ToastUtil.show("请选择生日")
                    birthdayTv.performClick()
                    HideUtil.closeKeyboard(this)
                    return
                }
                if (applyData["Addr"].isNullOrEmpty()) {
                    ToastUtil.show("请输入地址")
                    addressTv.requestFocus()
                    HideUtil.openKeyboard(this, addressTv)
                    return
                }
                if (applyData["ProvName"].isNullOrEmpty()) {
                    ToastUtil.show("请选择籍贯")
                    provinceTv.performClick()
                    HideUtil.closeKeyboard(this)
                    return
                }
                if (applyData["Marriege"].isNullOrEmpty()) {
                    ToastUtil.show("请选择婚姻状态")
                    marriegeTv.performClick()
                    HideUtil.closeKeyboard(this)
                    return
                }
                if (applyData["Edu"].isNullOrEmpty()) {
                    ToastUtil.show("请选择教育程度")
                    educationTv.performClick()
                    HideUtil.closeKeyboard(this)
                    return
                }
                mPresenter.submitApplyInfo(applyData)
            }
        }
    }
}